package com.example.user.customviewdemo.drawing.shader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.user.customviewdemo.R;

/**
 * Created by user on 2016/5/11.
 *
 */
public class BitmapShaderView extends View {
    private Paint paint;
    private final float RECT_WIDTH = 800;
    private final float RECT_HEIGHT = 800;
    private final float RADIUS = 100;
    private float x = 500,y = 600;

    public BitmapShaderView(Context context) {
        super(context);
    }

    public BitmapShaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeWidth(10);
        paint.setColor(Color.BLACK);

        /**                                    x方向                   y方向
         * BitmapShader (Bitmap bitmap, Shader.TileMode tileX, Shader.TileMode tileY)
         * TileMode.MIRROR 表示向镜子一样
         * TileMode.REPEAT 表示重复
         * TileMode.CLAMP 表示拉伸边缘第一个像素
         */
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.dog);
        //是先作用于Y轴，然后再作用于X轴，所以，如果交换位置，是看不到两种模式的效果的,就只能看到CLAMP的效果
        BitmapShader shader = new BitmapShader(bm, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        Matrix matrix = new Matrix();
        matrix.setTranslate(400,500);

        shader.setLocalMatrix(matrix);
        paint.setShader(shader);
    }

    public BitmapShaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(x,y,RADIUS,paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = event.getX();
        y = event.getY();
        invalidate();
        return true;
    }
}
